package com.selfdriving.bean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;

import com.selfdriving.BaseApplication;
import com.selfdriving.R;
import com.selfdriving.utils.TextUtils;

public class JsonResolveUtils
{
	/** 首页VF数据 **/
	private static final String HOMEVF = "home.json";
	// 附近个人的json文件名称
	private static final String NEARBY_PEOPLE = "nearby_people.json";
	private static final String MY_FRIENDS = "my_friends.json";
	private static final String MY_GROUPS = "my_groups.json";
	private static final String PROFILE = "profile/";
	private static final String SUFFIX = ".json";

	/** 解析首页VFJson数据 **/
	public static boolean resolveHomeVF(BaseApplication application)
	{
		if (application.homeList != null && application.homeList.isEmpty())
		{
			String json = TextUtils.getJson(
					application.getApplicationContext(), HOMEVF);
			if (json != null)
			{
				try
				{
					JSONArray array = new JSONArray(json);
					Home home = null;
					JSONObject object = null;
					for (int i = 0; i < array.length(); i++)
					{
						object = array.getJSONObject(i);
						String id = object.getString("id");
						String img = object.getString("img");
						home = new Home(id, img);
						application.homeList.add(home);
					}
				} catch (JSONException e)
				{
					e.printStackTrace();
					application.homeList.clear();
				}
			}
		}
		if (application.homeList.isEmpty())
		{
			return false;
		} else
		{
			return true;
		}
	}

	/**
	 * 解析我的好友Json数据
	 * 
	 * @param application
	 * @return
	 */
	public static boolean resolveMyFriends(BaseApplication application)
	{

		Map<String, Object> map;
		if (application.myFriends != null && application.myFriends.isEmpty())
		{
			String json = TextUtils.getJson(
					application.getApplicationContext(), MY_FRIENDS);
			if (json != null)
			{
				try
				{
					JSONArray array = new JSONArray(json);
					JSONObject itemObject = null;
					for (int i = 0; i < array.length(); i++)
					{
						map = new HashMap<String, Object>();
						List<NearByPeople> mNearByPeoples = new ArrayList<NearByPeople>();
						itemObject = array.getJSONObject(i);
						String groupName = itemObject.getString("groupName");
						JSONArray itemArray = itemObject
								.getJSONArray("groupItem");
						JSONObject object = null;
						NearByPeople people = null;
						map.put("name", groupName);
						for (int j = 0; j < itemArray.length(); j++)
						{
							object = itemArray.getJSONObject(j);
							String uid = object.getString(NearByPeople.UID);
							String avatar = object
									.getString(NearByPeople.AVATAR);
							int isVip = object.getInt(NearByPeople.VIP);
							int isGroupRole = object
									.getInt(NearByPeople.GROUP_ROLE);
							String industry = object
									.getString(NearByPeople.INDUSTRY);
							int isbindWeibo = object.getInt(NearByPeople.WEIBO);
							int isbindTxWeibo = object
									.getInt(NearByPeople.TX_WEIBO);
							int isbindRenRen = object
									.getInt(NearByPeople.RENREN);
							int device = object.getInt(NearByPeople.DEVICE);
							int isRelation = object
									.getInt(NearByPeople.RELATION);
							int isMultipic = object
									.getInt(NearByPeople.MULTIPIC);
							String name = object.getString(NearByPeople.NAME);
							int gender = object.getInt(NearByPeople.GENDER);
							int age = object.getInt(NearByPeople.AGE);
							String distance = object
									.getString(NearByPeople.DISTANCE);
							String time = object.getString(NearByPeople.TIME);
							String sign = object.getString(NearByPeople.SIGN);
							people = new NearByPeople(uid, avatar, isVip,
									isGroupRole, industry, isbindWeibo,
									isbindTxWeibo, isbindRenRen, device,
									isRelation, isMultipic, name, gender, age,
									distance, time, sign);
							mNearByPeoples.add(people);
						}
						map.put("item", mNearByPeoples);
						application.myFriends.add(map);
					}
				} catch (JSONException e)
				{
					application.myFriends.clear();
				}
			}
		}
		if (application.myFriends.isEmpty())
		{
			return false;
		} else
		{
			return true;
		}
	}

	/**
	 * 解析我的分组Json数据
	 * 
	 * @param application
	 * @return
	 */
	public static boolean resolveMyGroups(BaseApplication application)
	{

		if (application.myGroupsList != null
				&& application.myGroupsList.isEmpty())
		{
			String json = TextUtils.getJson(
					application.getApplicationContext(), MY_GROUPS);
			if (json != null)
			{
				try
				{
					JSONArray array = new JSONArray(json);
					MyGroups myGroups = null;
					JSONObject object = null;
					for (int i = 0; i < array.length(); i++)
					{
						object = array.getJSONObject(i);
						String icon = object.getString("icon");
						String name = object.getString("name");
						myGroups = new MyGroups(icon, name);
						application.myGroupsList.add(myGroups);
					}
				} catch (JSONException e)
				{
					application.myGroupsList.clear();
				}
			}
		}
		if (application.myGroupsList.isEmpty())
		{
			return false;
		} else
		{
			return true;
		}
	}

	/**
	 * 解析附近个人Json数据
	 * 
	 * @param application
	 * @return
	 */
	public static boolean resolveNearbyPeople(BaseApplication application)
	{
		if (application.mNearByPeoples != null
				&& application.mNearByPeoples.isEmpty())
		{
			String json = TextUtils.getJson(
					application.getApplicationContext(), NEARBY_PEOPLE);
			if (json != null)
			{
				try
				{
					JSONArray array = new JSONArray(json);
					NearByPeople people = null;
					JSONObject object = null;
					for (int i = 0; i < array.length(); i++)
					{
						object = array.getJSONObject(i);
						String uid = object.getString(NearByPeople.UID);
						String avatar = object.getString(NearByPeople.AVATAR);
						int isVip = object.getInt(NearByPeople.VIP);
						int isGroupRole = object
								.getInt(NearByPeople.GROUP_ROLE);
						String industry = object
								.getString(NearByPeople.INDUSTRY);
						int isbindWeibo = object.getInt(NearByPeople.WEIBO);
						int isbindTxWeibo = object
								.getInt(NearByPeople.TX_WEIBO);
						int isbindRenRen = object.getInt(NearByPeople.RENREN);
						int device = object.getInt(NearByPeople.DEVICE);
						int isRelation = object.getInt(NearByPeople.RELATION);
						int isMultipic = object.getInt(NearByPeople.MULTIPIC);
						String name = object.getString(NearByPeople.NAME);
						int gender = object.getInt(NearByPeople.GENDER);
						int age = object.getInt(NearByPeople.AGE);
						String distance = object
								.getString(NearByPeople.DISTANCE);
						String time = object.getString(NearByPeople.TIME);
						String sign = object.getString(NearByPeople.SIGN);

						people = new NearByPeople(uid, avatar, isVip,
								isGroupRole, industry, isbindWeibo,
								isbindTxWeibo, isbindRenRen, device,
								isRelation, isMultipic, name, gender, age,
								distance, time, sign);
						application.mNearByPeoples.add(people);
					}
				} catch (JSONException e)
				{
					application.mNearByPeoples.clear();
				}
			}
		}
		if (application.mNearByPeoples.isEmpty())
		{
			return false;
		} else
		{
			return true;
		}
	}

	public static boolean resolveNearbyProfile(Context context,
			NearByPeopleProfile profile, String uid)
	{
		if (!android.text.TextUtils.isEmpty(uid))
		{
			String json = TextUtils.getJson(context, PROFILE + uid + SUFFIX);
			if (json != null)
			{
				try
				{
					JSONObject object = new JSONObject(json);
					String userId = object.getString(NearByPeopleProfile.UID);
					String avatar = object
							.getString(NearByPeopleProfile.AVATAR);
					String name = object.getString(NearByPeopleProfile.NAME);
					int gender = object.getInt(NearByPeopleProfile.GENDER);
					int genderId = -1;
					int genderBgId = -1;
					if (gender == 0)
					{
						genderId = R.drawable.ic_user_famale;
						genderBgId = R.drawable.bg_gender_famal;
					} else
					{
						genderId = R.drawable.ic_user_male;
						genderBgId = R.drawable.bg_gender_male;
					}
					int age = object.getInt(NearByPeopleProfile.AGE);
					String constellation = object
							.getString(NearByPeopleProfile.CONSTELLATION);
					String distance = object
							.getString(NearByPeopleProfile.DISTANCE);
					String time = object.getString(NearByPeopleProfile.TIME);

					boolean isHasSign = false;
					String sign = null;
					String signPic = null;
					String signDis = null;

					if (object.has(NearByPeopleProfile.SIGNATURE))
					{
						isHasSign = true;
						JSONObject signObject = object
								.getJSONObject(NearByPeopleProfile.SIGNATURE);
						sign = signObject.getString(NearByPeopleProfile.SIGN);
						if (signObject.has(NearByPeopleProfile.SIGN_PIC))
						{
							signPic = signObject
									.getString(NearByPeopleProfile.SIGN_PIC);
						}
						signDis = signObject
								.getString(NearByPeopleProfile.SIGN_DIS);
					}

					JSONArray photosArray = object
							.getJSONArray(NearByPeopleProfile.PHOTOS);
					List<String> photos = new ArrayList<String>();
					for (int i = 0; i < photosArray.length(); i++)
					{
						photos.add(photosArray.getString(i));
					}
					profile.setUid(userId);
					profile.setAvatar(avatar);
					profile.setName(name);
					profile.setGender(gender);
					profile.setGenderId(genderId);
					profile.setGenderBgId(genderBgId);
					profile.setAge(age);
					profile.setConstellation(constellation);
					profile.setDistance(distance);
					profile.setTime(time);
					profile.setHasSign(isHasSign);
					profile.setSign(sign);
					profile.setSignPicture(signPic);
					profile.setSignDistance(signDis);
					profile.setPhotos(photos);
				} catch (JSONException e)
				{
					e.printStackTrace();
					profile = null;
					return false;
				}
				return true;
			}
		}
		return false;
	}
}
